package org.hgg.workflow.core;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Jarvis
 * @date 2023/12/28 0:18
 */
public class DefaultExecutorState<T, R> implements ExecutorState<T, R> {
    //底层图数据结构
    //已完成
    private final Set<Node<T, R>> processedNodes;
    //未完成
    private final Set<Node<T, R>> unProcessedNodes;
    //错误task
    private final Set<ExecutionResult<T, R>> erroredTasks;
    //执行结果
    private final Set<ExecutionResult<T, R>> executionResults;

    public DefaultExecutorState() {
        this.processedNodes = new HashSet<>();
        this.unProcessedNodes = new HashSet<>();
        this.erroredTasks = new HashSet<>();
        this.executionResults = new HashSet<>();
    }


    @Override
    public void markProcessingDone(Node<T, R> node) {
        this.processedNodes.add(node);
        this.unProcessedNodes.remove(node);
    }

    @Override
    public void markProcessingFailed(Node<T, R> node, ExecutionResult<T, R> result) {
        this.unProcessedNodes.remove(node);
        erroredTasks.add(result);
    }

    @Override
    public boolean isReady(Node<T, R> node) {
        return !processedNodes.contains(node) && processedNodes.containsAll(node.getParents());
    }
}
